package com.mio.hdfs.sort;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class SortDriver {
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		
		// 1 获取配置信息，或者job对象实例
		Configuration configuration = new Configuration(); 
		Job job = Job.getInstance(configuration);
		// 2 指定本程序的jar包所在的本地路径 
		job.setJarByClass(SortDriver.class);
		// 3 指定本业务job要使用的mapper/Reducer业务类 
		job.setMapperClass(SortMap.class); 
		job.setReducerClass(SortReduce.class);
		// 4 指定mapper输出数据的kv类型
		job.setMapOutputKeyClass(SortBean.class); 
		job.setMapOutputValueClass(NullWritable.class);
		// 5 指定最终输出的数据的kv类型
		job.setOutputKeyClass(SortBean.class); 
		job.setOutputValueClass(NullWritable.class);
		// 如果不设置InputFormat，它默认用的是TextInputFormat.class 
		job.setInputFormatClass(CombineTextInputFormat.class);
		//虚拟存储切片最大值设置4m 
		CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);
		// 6 指定job的输入原始文件所在目录
		FileInputFormat.setInputPaths(job, new Path("/Users/mio/Documents/sts-workspace/hdfsdemo/src/main/resources/input"));
		FileOutputFormat.setOutputPath(job, new Path("/Users/mio/Documents/sts-workspace/hdfsdemo/src/main/resources/out")); 
		// 7 指定分区器，指定分组比较器，设置reducetask数量 
//		job.setPartitionerClass(CustomPartitioner.class); 
//		job.setGroupingComparatorClass(CustomGroupingComparator.class); 
//		job.setNumReduceTasks(2);
		// 8 将job中配置的相关参数，以及job所用的java类所在的jar包， 提交给yarn去运行 
		boolean result = job.waitForCompletion(true);
		System.exit(result ? 0 : 1);
	

	}

}
